/*************************************************************************
* Copyright(C),2017,ZDC Corporation.
* All right reserved.
* 
* File Name: chat_fun.c
* Description:
* Author: mdc
* Mail: 1436208384@qq.com
* Created Time: 2017年09月15日 星期五 15时20分01秒
************************************************************************/

#include "chat.h"
/****************屏幕初始化**************/
void screen_init(void)
{
    if (NULL == initscr())
    {
        perror("initscr");
        exit(EXIT_FAILURE);
    }
    /*调用start_color显示色彩*/
    start_color();
    init_pair(1,COLOR_WHITE,COLOR_BLUE);
    init_pair(2,COLOR_BLUE,COLOR_WHITE);
    init_pair(3,COLOR_RED,COLOR_WHITE);
    keypad(stdscr,TRUE); //可在win中接收键盘功能键,会造成段错误？
    //keypad(mainwin,TRUE); //可在win中接收键盘功能键
    //curs_set(0);      //屏蔽物理指针,光标会不闪烁
    echo();          //设置终端回显

}
/****************登录界面绘制********************/
int ui_login(void)   //疑问，好像不用传参
{
    int y,x;
    screen_init();    // 初始化窗口
    WINDOW *lgwin;
    getmaxyx(stdscr,y,x);
    if(NULL == (lgwin = newwin(16,40,y/2-14/2,x/2-34/2)))
    {
        endwin();
        exit(-1);
    }
    wbkgd(lgwin,COLOR_PAIR(2));
    refresh();
    box(lgwin,0,0);    //环绕窗口绘制边框
    getmaxyx(lgwin,y,x);
    mvwhline(lgwin,4,1,0,x-2);
    mvwaddstr(lgwin,y/2-6,x/2-3,"Login");
    wrefresh(lgwin);
    mvwprintw(lgwin,6,5, "Name:");  //在终端打印name和password
    mvwprintw(lgwin,8,5, "password:"); 
    //mvwprintw(lgwin,10,16, "<Login>");  //在终端打印login
    wattron(lgwin,COLOR_PAIR(3));
    wrefresh(lgwin);
    wmove(lgwin,6,11);      //光标移动到此位置
    // wscanw(lgwin,"%s",name); //接收输入的字符存入字符数组name
    wgetstr(lgwin,name);
    wmove(lgwin,8,15);
    //wscanw(lgwin,"%s",pwd);  //接收输入的字符存入字符数组pwd
    wgetstr(lgwin,pwd);      //接收用户输入的密码
    strcpy(md5lgpwd,MD5String(pwd));//将明文加密后的密文存入md5lgpwd
    // getch();
    // curs_set(1);
    noecho();
    erase();
    delwin(lgwin);
    endwin();
    refresh();
    return 0;
}
/*********************编辑窗口绘制********************/
void editwin_init(WINDOW *editwin, int y, int x)
{
    mvwprintw(editwin,8,1,"Menu"); 
    mvwprintw(editwin,8,30,"config"); 
    mvwprintw(editwin,8,95,"Quit"); 
    keypad(editwin,TRUE); //可在win中接收键盘功能键
    wbkgd(editwin,COLOR_PAIR(2));
    box(editwin,0,0);   
    mvwhline(editwin,6,1,0,100);
    refresh();
    mvwprintw(editwin,1,1, "Edit:");
    wattron(editwin,COLOR_PAIR(3));
    wrefresh(editwin);
    /*绘制结束*/
}
/**********************发送窗口绘制********************/
void sentwin_init(WINDOW *sentwin, int y, int x)
{
    keypad(sentwin,TRUE); //可在win中接收键盘功能键
    box(sentwin,0,0);   
    wbkgd(sentwin,COLOR_PAIR(2));
    //scrollok(sentwin,TRUE);
    //wsetscrreg(sentwin, 2, 16);
    //idlok(sentwin,TRUE);
    cbreak();
    //wscrl(sentwin,4);
    refresh();
    mvwprintw(sentwin,1,22, "Sent");  
    wattron(sentwin,COLOR_PAIR(3));
    wrefresh(sentwin);

}
/**********************接收窗口绘制********************/
void recvwin_init(WINDOW *recvwin, int y, int x)
{
    keypad(recvwin,TRUE); //可在win中接收键盘功能键
    box(recvwin,0,0);   
    wbkgd(recvwin,COLOR_PAIR(2));
    refresh();
    mvwprintw(recvwin,1,18,"Received"); 
    wattron(recvwin,COLOR_PAIR(3));
    wrefresh(recvwin);
}
/*********************配置窗口绘制********************/
void cfgwin_init(WINDOW *cfgwin, int y, int x)
{
    mvwprintw(cfgwin,1,22, "Config");
    mvwhline(cfgwin,2,1,0,49);
    mvwprintw(cfgwin,3,5,"Name:"); 
    mvwprintw(cfgwin,3,10,name_cfg);
    mvwprintw(cfgwin,5,5,"Password:"); 
    mvwprintw(cfgwin,5,14,pwd);
    mvwprintw(cfgwin,7,5,"IPAddress:"); 
    mvwprintw(cfgwin,7,15,ip_cfg);
    mvwprintw(cfgwin,9,5,"sentPort:"); 
    mvwprintw(cfgwin,9,14,port_cfg);
    mvwprintw(cfgwin,11,5,"ListenPort:"); 
    mvwprintw(cfgwin,11,16,lport_cfg);
    mvwprintw(cfgwin,13,5,"Connect:"); 
    mvwprintw(cfgwin,13,13,connect_cfg);
    mvwprintw(cfgwin,15,15,"[Save]"); 
    mvwprintw(cfgwin,15,30,"[Quit]"); 
    keypad(cfgwin,TRUE); //可在win中接收键盘功能键
    wbkgd(cfgwin,COLOR_PAIR(2));
    box(cfgwin,0,0);   
    refresh();
    wattron(cfgwin,COLOR_PAIR(3));
    wrefresh(cfgwin);
    /*绘制结束*/
}

/*********************TCP接收函数*********************/
void *tcpserver(void* arg)
{
    top = 0;    //打印消息时的起始和结束位置
    end = 15;
    cur = 0;    //打印消息的当前位置
    line = 2;  //在窗口上打印消息的起始行数
    int tlistenfd;   //监听套接字
    int tconnfd;    //连接后的套接字
    struct sockaddr_in servaddr,cliaddr;
    socklen_t peerlen;
    char ttransbuf[128];//tcp传输用的buf
    //建立socket连接
    if (-1 == (tlistenfd = socket(AF_INET,SOCK_STREAM,0)))
    {
        perror("socket");
        exit(-1);
    }
    printf("tlistenfd is %d\n",tlistenfd);
    //设置sockaddr_in结构体中相关的参数
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(atoi(lport_cfg));
    servaddr.sin_addr.s_addr = INADDR_ANY;
    //绑定函数
    if (bind(tlistenfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) < 0)
    {
        perror("bind");
        exit(-1);
    }

    printf("tcp bind success\n");
    //调用listen函数，设置监听模式
    if (-1 == listen(tlistenfd,10))
    {
        perror("listen");
        exit(-1);
    }
    printf("listening\n");
    //调用accept函数，等待客户端的连接
    peerlen = sizeof(cliaddr);
    r_msg_num = 0;//输入消息的总条目数,不能放在死循环里
    while(1)
    {
        //printf("waiting to accept\n");
        if ((tconnfd = accept(tlistenfd,(struct sockaddr *)&cliaddr,&peerlen)) < 0)
        {
            perror("accept");
            exit(-1);
        }
        //printf("accept success\n");
        //调用recv函数接收发送端发来的数据
        memset(ttransbuf,0,sizeof(ttransbuf));
        if (-1 == recv(tconnfd,ttransbuf,128,0))
        {
            perror("recv");
            exit(-1);
        }
        strcpy(recvemsg,ttransbuf);   //收到的消息拷贝到recvmsg中
        strcpy(allrecvmsg[r_msg_num],recvemsg);//每一条消息存到一个二维数组中
        r_msg_num++;             //消息总条目数++
        memset(recvemsg,0,sizeof(recvemsg));
                // 接收框的刷新显示
                line = 2;
                //rtop = 0;       //top,end重新赋值
                //rend = 15;
                for(cur = top;cur < end;cur++)              //从allmsg[top]开始循环打印到allmsg[end]
                {
                    mvwprintw(recvwin,line,1,allrecvmsg[cur]);  //将内容打印到接收窗口
                    line++;
                }
                //top = 0;       //top,end重新赋值
                //end = 15;
                if (r_msg_num > 15) //如果检测到输入消息的总行数超过了屏幕高度
                {
                    top = 0 + (r_msg_num - 14);   
                    end = 15 + (r_msg_num - 14); 
                }
                wrefresh(recvwin);
        //wrefresh(recvwin);
        //printf("message %s received\n",recvemsg);

        close(tconnfd);
    }
    close(tlistenfd);
    //exit(0);
    pthread_exit(NULL);
}

/*********************TCP发送函数*********************/
void tcpclient(void)
{
    int tsockfd;    //连接后的套接字
    struct sockaddr_in servaddr;
    char ttransbuf[128];//tcp传输用的buf
    //建立socket连接
    if (-1 == (tsockfd = socket(AF_INET,SOCK_STREAM,0)))
    {
        perror("socket");
        exit(-1);
    }
    //printf("tlistenfd is %di\n",tlistenfd);
    //设置sockaddr_in结构体中相关的参数
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(atoi(port_cfg));
    servaddr.sin_addr.s_addr = inet_addr(ip_cfg);
    //printf("going to connect\n");
    //调用connect向服务器请求
    if (-1 == connect(tsockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)))
    {
        perror("connect");
        sleep(2);
        endwin();  //删除窗口资源，否则终端会出错
        //erase();
        //refresh();
        //printf("connect failed\n");
        printf("server not runing\n");
        exit(-1);
    }
    //printf("connect sueeess\n");

    strcpy(ttransbuf,sentmsg);
    send(tsockfd,ttransbuf,sizeof(ttransbuf),0);
    //printf("message %s sent",ttransbuf);

    close(tsockfd);
    //exit(0);
}
/******************UDP接收函数*******************/
void *udpserver(void* arg)
{
    top = 0;    //打印消息时的起始和结束位置
    end = 15;
    cur = 0;    //打印消息的当前位置
    line = 2;  //在窗口上打印消息的起始行数
    int usockfd;
    struct sockaddr_in servaddr,cliaddr;
    socklen_t peerlen;
    char utransbuf[128];
    //建立socket连接
    if (-1 == (usockfd = socket(AF_INET,SOCK_DGRAM,0)))
    {
        perror("socket");
        exit(-1);
    }
    printf("usockfd is %d\n",usockfd);
    //设置sockaddr_in结构体中相关参数
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(atoi(lport_cfg));
    servaddr.sin_addr.s_addr = INADDR_ANY;
    //绑定函数bind
    if (bind(usockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)) < 0)
    {
        perror("bind");
        exit(-1);
    }
    printf("udp bind success\n");
    //调用recvfrom函数等待接收客户端的数据
    peerlen = sizeof(cliaddr);
    while (1)
    {
        if (recvfrom(usockfd,utransbuf,sizeof(utransbuf),0,(struct sockaddr *)&cliaddr,&peerlen) < 0)
        {
            perror("recvfrom");
            exit(-1);
        }
        strcpy(recvemsg,utransbuf);//收到的消息放到recvemsg中
        strcpy(allrecvmsg[r_msg_num],recvemsg);//每收到一条消息存到二维数组里
        r_msg_num++;
        //printf("udp received message %s\n",recvemsg);
                line = 2;
                //rtop = 0;       //top,end重新赋值
                //rend = 15;
                for(cur = top;cur < end;cur++)              //从allmsg[top]开始循环打印到allmsg[end]
                {
                    mvwprintw(recvwin,line,1,allrecvmsg[cur]);  //将内容打印到接收窗口
                    line++;
                }
                //top = 0;       //top,end重新赋值
                //end = 15;
                if(r_msg_num > 15) //如果检测到输入消息的总行数超过了屏幕高度
                {
                    top = 0 + (r_msg_num - 14);   
                    end = 15 + (r_msg_num - 14); 
                }
                wrefresh(recvwin);
    }
    close(usockfd);
    pthread_exit(NULL);
    
}
/*********************UDP发送函数*********************/
void udpclient(void)
{
    int usockfd;    //连接后的套接字
    struct sockaddr_in servaddr;
    char utransbuf[128];//tcp传输用的buf
    //建立socket连接
    if (-1 == (usockfd = socket(AF_INET,SOCK_DGRAM,0)))
    {
        perror("socket");
        exit(-1);
    }
    //printf("usockfd is %di\n",usockfd);
    //设置sockaddr_in结构体中相关的参数
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(atoi(port_cfg));
    servaddr.sin_addr.s_addr = inet_addr(ip_cfg);
    //printf("going to connect\n");

    //发送消息给服务端
    strcpy(utransbuf,sentmsg);
    sendto(usockfd,utransbuf,sizeof(utransbuf),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
    //printf("message %s sent",utransbuf);
    close(usockfd);
    //exit(0);
}

/********************聊天主界面绘制********************/
int ui_chat(void)
{
    int y,x;
    int msg_num = 0;//输入消息的总条目数
    r_msg_num = 0;//输入消息的总条目数
    int sentflag = 0;
    int recvflag = 0;
    top = 0;    //打印消息时的起始和结束位置
    end = 15;
    cur = 0;    //打印消息的当前位置
    line = 2;  //在窗口上打印消息的起始行数
    int fun_key;   //功能键
    //FILE *rcd;
    char sentbuf[128];//定义发送暂存buf
    screen_init();  
    WINDOW *editwin;
    WINDOW *sentwin;
    //WINDOW *recvwin;//为了能在本函数外执行刷新窗口，定义为全局变量了
    //WINDOW *cfgwin;
    getmaxyx(stdscr,y,x);
    /*创建3个独立的窗口 编辑窗口，发送窗口，接收窗口*/
    if (NULL == (editwin = newwin(10,102,y / 2 + y / 7 , x / 5 )))
    {
        endwin();
        exit(-1);
    }
    if (NULL == (sentwin = newwin(18,52,y / 7, x / 5)))
    {
        endwin();
        exit(-1);
    }
    if (NULL == (recvwin = newwin(18,50,y / 7, x / 5 + 52 )))
    {
        endwin();
        exit(-1);
    }
    //winsinit:
    editwin_init(editwin,y,x);//编辑框属性初始化  
    sentwin_init(sentwin,y,x);//发送框属性初始化
    recvwin_init(recvwin,y,x);//接受框属性初始化

    while ((fun_key = getch()))
    {
        switch (fun_key)
        {
        case KEY_F(2):
            do
            {
                wmove(editwin,1,7);         //光标移动到编辑框位置
                wgetstr(editwin,sentmsg);
                werase(editwin);          //清除编辑窗口内容
                editwin_init(editwin,y,x);//重新设置编辑窗口属性
                werase(sentwin);          //清除窗口内容
                sentwin_init(sentwin,y,x);//重新设置编辑窗口属性
                /*超出窗口屏幕滚动下翻*/
                strcpy(allsentmsg[msg_num],sentmsg);//将存放消息的msg复制到二维数组的一行
                //strcpy(allrecvmsg[msg_num],sentmsg);//将存放消息的msg复制到二维数组的一行
                msg_num++;                  
                //根据连接方式的不同将消息发送出去
                if (0 == strcmp(connect_cfg,"0"))
                {
                    tcpclient();//将消息发送出去
                }
                else if (0 == strcmp(connect_cfg,"1"))
                {
                    udpclient();
                }
                line = 2;
                //top = 0;    //打印消息时的起始和结束位置
                //end = 15;
                for(cur = top;cur < end;cur++)              //从allmsg[top]开始循环打印到allmsg[end]
                {
                    mvwprintw(sentwin,line,1,allsentmsg[cur]);  //将内容打印到发送窗口
                    line++;
                }
                //top = 0;       //top,end重新赋值
                //end = 15;
                if(msg_num > 15) //如果检测到输入消息的总行数超过了屏幕高度
                {
                    top = 0 + (msg_num - 14);   
                    end = 15 + (msg_num - 14); 
                }
                //mvwprintw(sentwin,line,1,sentmsg);  //将内容打印到发送窗口
                wrefresh(sentwin);
               /* // 接收框的刷新显示
                line = 2;
                //rtop = 0;       //top,end重新赋值
                //rend = 15;
                for(cur = top;cur < end;cur++)              //从allmsg[top]开始循环打印到allmsg[end]
                {
                    mvwprintw(recvwin,line,1,allrecvmsg[cur]);  //将内容打印到接收窗口
                    line++;
                }
                //top = 0;       //top,end重新赋值
                //end = 15;
                if(r_msg_num > 15) //如果检测到输入消息的总行数超过了屏幕高度
                {
                    top = 0 + (r_msg_num - 14);   
                    end = 15 + (r_msg_num - 14); 
                }
                wrefresh(recvwin);*/
                
            }while (0 != strcmp(sentmsg,"."));
            break;
        case KEY_F(3):
            wmove(sentwin,1,22);         //光标移动到sentwin框位置
            wrefresh(sentwin);
            /*屏幕滚动上下翻*/
            int updown; //定义接收上下方向键的变量
            while (0 != (updown = getch()))
            {
                line = 2;                 //重新打印起始行数赋值
                werase(sentwin);          //清除窗口内容
                sentwin_init(sentwin,y,x);//重新设置发送窗口属性
                wrefresh(sentwin);
                switch (updown)
                {
                case KEY_UP:
                    if(top > 0)         //top最小不能小于0
                    {
                        top--; 
                        end--; 
                    }
                    //wprintw(sentwin,"test");
                    for(cur = top;cur < end;cur++)
                    {
                        mvwprintw(sentwin,line,1,allsentmsg[cur]);  //将内容打印到发送窗口
                        line++;
                    }
                    break;
                case KEY_DOWN:
                    if(end < msg_num)   //end最大不能超过信息总行数
                    {
                        top++;
                        end++; 
                    }
                    for(cur = top;cur < end;cur++)
                    {
                        mvwprintw(sentwin,line,1,allsentmsg[cur]);  //将内容打印到发送窗口
                        line++;
                    }
                    break;
                default:
                    sentflag = QUIT;//如果输入不是上下键，将sentflag位置为QUIT
                    break;
                }
                //mvwprintw(sentwin,line,1,sentmsg); //将内容打印到发送窗口
                //break;
                wrefresh(sentwin);  //必须写，不然显示不出来
                //如果flag为QUIT，跳出while循环
                if (QUIT == sentflag)
                {
                    break;
                }
            }
            break;
        case KEY_F(4):
            wmove(recvwin,1,18);         //光标移动到recvwin框位置
            wrefresh(recvwin);
            /*屏幕滚动上下翻*/
            int r_updown; //定义接收上下方向键的变量
            while (0 != (r_updown = getch()))
            {
                line = 2;                 //重新打印起始行数赋值
                werase(recvwin);          //清除窗口内容
                recvwin_init(recvwin,y,x);//重新设置发送窗口属性
                wrefresh(recvwin);
                switch (r_updown)
                {
                    case KEY_UP:
                    if(top > 0)         //top最小不能小于0
                    {
                        top--; 
                        end--; 
                    }
                    //wprintw(sentwin,"test");
                    for(cur = top;cur < end;cur++)
                    {
                        mvwprintw(recvwin,line,1,allrecvmsg[cur]);  //将内容打印到发送窗口
                        line++;
                    }
                    break;
                    case KEY_DOWN:
                    if(end < r_msg_num)   //end最大不能超过信息总行数
                    {
                        top++;
                        end++; 
                    }
                    for(cur = top;cur < end;cur++)
                    {
                        mvwprintw(recvwin,line,1,allrecvmsg[cur]);  //将内容打印到发送窗口
                        line++;
                    }
                    break;
                    default:
                    recvflag = QUIT;
                    break;
                }
                //mvwprintw(sentwin,line,1,sentmsg); //将内容打印到发送窗口
                //break;
                wrefresh(recvwin);  //必须写，不然显示不出来
                if (recvflag == QUIT)
                {
                    break;
                }
            }
            break;
        case KEY_F(5):
            wmove(editwin,8,1);         //光标移动到menu位置
            wrefresh(editwin);
            break;
        case KEY_F(6):
            wmove(editwin,8,30);    //光标移动到config位置
            wrefresh(editwin);
            int f6_opt;
            f6_opt = getch();
            switch (f6_opt)
            {
                case 10: 
                delwin(sentwin) ;
                delwin(recvwin) ;
                delwin(editwin) ;
                endwin();
                erase();
                refresh();
                return CONFIG;
                break;
                default:
                break;
            }
            break;
        case KEY_F(7):
            wmove(editwin,8,95);     //光标移动到quit位置
            wrefresh(editwin);
            int quit;
            quit = getch();
            switch (quit)
            {
                case 10:
                //wprintw(recvwin,"this a config test");
                //wrefresh(recvwin);
                delwin(editwin);
                delwin(sentwin);
                delwin(recvwin);
                //delwin(editwin);
                endwin();
                exit(0);
                break;
                default:
                break;
            }
            //mvwprintw(sentwin,line,1,"UP"); 
        default:
                break;
    }
    }
    //getch();
    //curs_set(1);
    //noecho();
    //delwin(editwin);
    //endwin();
    return 0;
}
/********************config界面弹出并修改******************/
int config(void)
{
    int enter;
    int option;
    int y,x;
    WINDOW *cfgwin;
    getmaxyx(stdscr,y,x);
    //while(enter = getch())
    //while(1)
    // {
    //if(10 == enter)     //回车键的ascii码为10
    //{
    if (NULL == (cfgwin = newwin(18,50,y / 5 , x / 3  )))
    {
        endwin();
        exit(-1);
    }
    //接收用户修改配置
    cfgwin_init(cfgwin,y,x); //cfgwin窗口属性初始化
    //wrefresh(recvwin);
    wmove(cfgwin,3,10);    //光标移动到name位置
    wgetstr(cfgwin,namecfgbuf);
    wmove(cfgwin,5,14);    //光标移动到password位置
    wgetstr(cfgwin,pwdcfgbuf);

    strcpy(md5cfgpwd,MD5String(pwdcfgbuf));//修改后的密码进行md5加密后存入md5cfgpwd

    wmove(cfgwin,7,15);    //光标移动到ipaddress位置
    wgetstr(cfgwin,ipcfgbuf);
    wmove(cfgwin,9,14);    //光标移动到sentport位置
    wgetstr(cfgwin,portcfgbuf);
    wmove(cfgwin,11,16);    //光标移动到listenport位置
    wgetstr(cfgwin,lportcfgbuf);
    wmove(cfgwin,13,13);    //光标移动到connect位置
    wgetstr(cfgwin,connectcfgbuf);
    wmove(cfgwin,15,16);    //光标移动到save位置
    wrefresh(cfgwin);
    while (27 != (option = getch()))
    {
        switch (option)
        {
        case KEY_LEFT:
            wmove(cfgwin,15,16);    //光标移动到save位置
            // wprintw(cfgwin,"save hello");  //将内容打印到发送窗口
            int confirm;
            FILE *cfg;
            char cfgbuf[64] = {0};
            wrefresh(cfgwin);

            confirm = getch();
            switch (confirm)
            {
            case 10:
                //wprintw(cfgwin,"save test");  //将内容打印到发送窗口
                wrefresh(cfgwin);
                if (NULL == (cfg = fopen(".config","w+")))
                {
                    perror("fopen");
                    //fclose(cfg);
                    return -1;
                }

                //修改信息写入
                fwrite(namecfgbuf,sizeof(char),strlen(namecfgbuf),cfg);  //修改用户名写入
                fwrite("\n",sizeof(char),1,cfg);  
                //fwrite(pwdcfgbuf,sizeof(char),strlen(pwdcfgbuf),cfg);  //修改密码写入
                //fwrite("\n",sizeof(char),1,cfg);  
                fwrite(md5cfgpwd,sizeof(char),strlen(md5cfgpwd),cfg);  //修改后并加密的密码写入
                fwrite("\n",sizeof(char),1,cfg);  
                fwrite(ipcfgbuf,sizeof(char),strlen(ipcfgbuf),cfg);  //修改ip写入
                fwrite("\n",sizeof(char),1,cfg);  
                fwrite(portcfgbuf,sizeof(char),strlen(portcfgbuf),cfg);  //修改发送端口号写入
                fwrite("\n",sizeof(char),1,cfg);  
                fwrite(lportcfgbuf,sizeof(char),strlen(lportcfgbuf),cfg);  //修改监听端口号写入
                fwrite("\n",sizeof(char),1,cfg);  
                fwrite(connectcfgbuf,sizeof(char),strlen(connectcfgbuf),cfg);  //修改连接方式写入
                fwrite("\n",sizeof(char),1,cfg);  
                //fclose(cfg);

                //重新将修改后的信息读入buf以便在程序运行时使得更改生效 
                /*if (NULL == (cfg = fopen(".config","r")))
                {
                perror("fopen");
                //fclose(cfg);
                return -1;
            }*/
                fseek(cfg,0,SEEK_SET);//重要！要想从头开始读必须把读写指针重新移到文件头，否则会段错误
                memset(name_cfg,0,sizeof(name_cfg));
                memset(pwd_cfg,0,sizeof(pwd_cfg));
                memset(ip_cfg,0,sizeof(ip_cfg));
                memset(port_cfg,0,sizeof(port_cfg));
                memset(lport_cfg,0,sizeof(lport_cfg));
                memset(connect_cfg,0,sizeof(connect_cfg));
                fgets(cfgbuf,64,cfg);
                strncpy(name_cfg,cfgbuf,strlen(cfgbuf)-1);
                fgets(cfgbuf,64,cfg);
                strncpy(pwd_cfg,cfgbuf,strlen(cfgbuf)-1);
                fgets(cfgbuf,64,cfg);
                strncpy(ip_cfg,cfgbuf,strlen(cfgbuf)-1);
                fgets(cfgbuf,64,cfg);
                strncpy(port_cfg,cfgbuf,strlen(cfgbuf)-1);
                fgets(cfgbuf,64,cfg);
                strncpy(lport_cfg,cfgbuf,strlen(cfgbuf)-1);
                fgets(cfgbuf,64,cfg);
                strncpy(connect_cfg,cfgbuf,strlen(cfgbuf)-1);
                fclose(cfg);
                //wprintw(cfgwin,name_cfg);//测试改完后的name_cfg是否成功
//wprintw(cfgwin,ip_cfg);//测试改完后的ip_cfg是否成功

                break;
            default:
                break;
            }
            break;
        case KEY_RIGHT:
            wmove(cfgwin,15,31);    //光标移动到quit位置
            wrefresh(cfgwin);
            int confirm2;
            confirm2 = getch();
            switch (confirm2)
            {
                case 10:
                //wprintw(cfgwin,"quit test");  //将内容打印到发送窗口
                wrefresh(cfgwin);
                delwin(cfgwin);
                erase();
                endwin();
                refresh();
                break;
                default:
                break;
            }
            break;
        default:
            break;
        }
        }
        return 0;
}
/*else
{
break;
        }
        }
        */
        // }

/****************运行时的聊天函数chating*************/
int chating(void)    
{
    int ret;
    while (1)
    {
        if (CONFIG == (ret = ui_chat()))
        { 
            config();
        }
    }
    return 0;
}
/*****************读取配置文件config******************/
int read_config(void)
{
    FILE *cfg;
    char cfgbuf[64] = {0};
    strcpy(namecfgbuf,"mdc");
    strcpy(pwdcfgbuf,"123");
    strcpy(ipcfgbuf,"0.0.0.0");
    strcpy(portcfgbuf,"8888");
    strcpy(lportcfgbuf,"8888");
    strcpy(connectcfgbuf,"0");//0表示tcp方式，1表示udp方式

    strcpy(md5pwd,MD5String(pwdcfgbuf));//将密码加密后存入buf
    /*
    * char namecfgbuf[32] =  {"mdc"};
    char pwdcfgbuf[32] = {"123"};
    char ipcfgbuf[32] =  {"192.168.82.12"};
    char portcfgbuf[32] =  {"8888"};
    char connectcfgbuf[32] =  {"0"};
    */
    //if(-1 == access(".config",0)) //判断文件是否存在
    if (NULL == (cfg = fopen(".config","r+")))
    {
        if (NULL == (cfg = fopen(".config","w+")))
        {
            perror("fopen");
            //fclose(cfg);
            return -1;
        }
        /*初始信息写入*/
        fwrite(namecfgbuf,sizeof(char),strlen(namecfgbuf),cfg);  //初始用户名写入
        fwrite("\n",sizeof(char),1,cfg);  
        //fwrite(pwdcfgbuf,sizeof(char),strlen(pwdcfgbuf),cfg);  //初始密码写入
        //fwrite("\n",sizeof(char),1,cfg);  
        fwrite(md5pwd,sizeof(char),strlen(md5pwd),cfg);  //加密后的密码写入
        fwrite("\n",sizeof(char),1,cfg);  
        fwrite(ipcfgbuf,sizeof(char),strlen(ipcfgbuf),cfg);  //初始ip写入
        fwrite("\n",sizeof(char),1,cfg);  
        fwrite(portcfgbuf,sizeof(char),strlen(portcfgbuf),cfg);  //初始端口号写入
        fwrite("\n",sizeof(char),1,cfg);  
        fwrite(lportcfgbuf,sizeof(char),strlen(lportcfgbuf),cfg);  //初始端口号写入
        fwrite("\n",sizeof(char),1,cfg);  
        fwrite(connectcfgbuf,sizeof(char),strlen(connectcfgbuf),cfg);  //初始连接方式写入
        fwrite("\n",sizeof(char),1,cfg);  
    }
    fseek(cfg,0,SEEK_SET);//重要！要想从头开始读必须把读写指针重新移到文件头，否则会段错误
    fgets(cfgbuf,64,cfg);
    strncpy(name_cfg,cfgbuf,strlen(cfgbuf)-1);
    fgets(cfgbuf,64,cfg);
    strncpy(pwd_cfg,cfgbuf,strlen(cfgbuf)-1);
    fgets(cfgbuf,64,cfg);
    strncpy(ip_cfg,cfgbuf,strlen(cfgbuf)-1);
    fgets(cfgbuf,64,cfg);
    strncpy(port_cfg,cfgbuf,strlen(cfgbuf)-1);
    fgets(cfgbuf,64,cfg);
    strncpy(lport_cfg,cfgbuf,strlen(cfgbuf)-1);
    fgets(cfgbuf,64,cfg);
    strncpy(connect_cfg,cfgbuf,strlen(cfgbuf)-1);

fclose(cfg);
return 0;
}
